package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.HttpClient5Utils;
import com.common.utils.StringUtil;
import com.entity.SharesDetail;
import com.entity.SharesYjyg;
import com.mapper.SharesYjygMapper;
import com.service.SharesDetailService;
import com.service.SharesYjygService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wty
 * @since 2021-11-21
 */
@Service
public class SharesYjygServiceImpl extends ServiceImpl<SharesYjygMapper, SharesYjyg> implements SharesYjygService {

    @Autowired
    private SharesDetailService sharesDetailService;

    @Override
    public void getyjyg() {
        Wrapper<SharesYjyg> dwr=new EntityWrapper<>();
        this.delete(dwr);
        List<SharesYjyg> sharesYjygs=new ArrayList<>();
        Wrapper<SharesDetail> wr=new EntityWrapper<>();
        List<SharesDetail> sharesDetails = sharesDetailService.selectList(wr);
        for (SharesDetail sharesDetail : sharesDetails) {
            String code = sharesDetail.getCode();
            String endCode = code.substring(2);
            String url="https://datacenter-web.eastmoney.com/securities/api/data/v1/get" +
                    "?callback=jQuery11230825487787365518_1640766627299" +
                    "&sortColumns=NOTICE_DATE&sortTypes=-1" +
                    "&pageSize=5&pageNumber=1&sty=ALL" +
                    "&filter=(SECURITY_CODE%3D%22"+endCode +"%22)" +
                    "&token=894050c76af8597a853f5b408b759f5d&type=RPT_PUBLIC_OP_NEWPREDICT" +
                    "&st=REPORT_DATE&reportName=RPT_PUBLIC_OP_NEWPREDICT&columns=ALL";

            //发送请求
            try {
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("result");
                String klines = JSONObject.parseObject(data).getString("data");
                List<Object> parse = (List<Object>) JSON.parse(klines);


                for (Object o : parse) {
                    //        : -17.86

                    SharesYjyg sharesYjyg=new SharesYjyg();

                    String ADD_AMP_LOWER = JSONObject.parseObject(o.toString()).getString("ADD_AMP_LOWER");
                    String ADD_AMP_UPPER = JSONObject.parseObject(o.toString()).getString("ADD_AMP_UPPER");
                    String CHANGE_REASON_EXPLAIN = JSONObject.parseObject(o.toString()).getString("CHANGE_REASON_EXPLAIN");
                    String FORECAST_JZ = JSONObject.parseObject(o.toString()).getString("FORECAST_JZ");
                    String FORECAST_STATE = JSONObject.parseObject(o.toString()).getString("FORECAST_STATE");
                    String INCREASE_JZ = JSONObject.parseObject(o.toString()).getString("INCREASE_JZ");
                    String IS_LATEST = JSONObject.parseObject(o.toString()).getString("IS_LATEST");
                    String NOTICE_DATE = JSONObject.parseObject(o.toString()).getString("NOTICE_DATE");
                    String ORG_CODE = JSONObject.parseObject(o.toString()).getString("ORG_CODE");
                    String PREDICT_AMT_LOWER = JSONObject.parseObject(o.toString()).getString("PREDICT_AMT_LOWER");
                    String PREDICT_AMT_UPPER = JSONObject.parseObject(o.toString()).getString("PREDICT_AMT_UPPER");
                    String PREDICT_CONTENT = JSONObject.parseObject(o.toString()).getString("PREDICT_CONTENT");
                    String PREDICT_FINANCE = JSONObject.parseObject(o.toString()).getString("PREDICT_FINANCE");
                    String PREDICT_FINANCE_CODE = JSONObject.parseObject(o.toString()).getString("PREDICT_FINANCE_CODE");
                    String PREDICT_TYPE = JSONObject.parseObject(o.toString()).getString("PREDICT_TYPE");
                    String PREYEAR_SAME_PERIOD = JSONObject.parseObject(o.toString()).getString("PREYEAR_SAME_PERIOD");
                    String REPORT_DATE = JSONObject.parseObject(o.toString()).getString("REPORT_DATE");
                    String SECURITY_NAME_ABBR = JSONObject.parseObject(o.toString()).getString("SECURITY_NAME_ABBR");
                    String SECURITY_CODE = JSONObject.parseObject(o.toString()).getString("SECURITY_CODE");

                    sharesYjyg.setForecastState(FORECAST_STATE);
                    sharesYjyg.setForecastJz(FORECAST_JZ);
                    if(!StringUtil.isEmpty(CHANGE_REASON_EXPLAIN)){
                        if(CHANGE_REASON_EXPLAIN.length()>50){
                            sharesYjyg.setChangeReasonExplain(CHANGE_REASON_EXPLAIN.substring(0,50));
                        }else {
                            sharesYjyg.setChangeReasonExplain(CHANGE_REASON_EXPLAIN);
                        }
                    }

                    sharesYjyg.setAddAmpLower(ADD_AMP_LOWER);
                    sharesYjyg.setAddAmpUpper(ADD_AMP_UPPER);
                    sharesYjyg.setOrgCode(ORG_CODE);
                    sharesYjyg.setNoticeDate(NOTICE_DATE);
                    sharesYjyg.setIsLatest(IS_LATEST);
                    sharesYjyg.setIncreaseJz(INCREASE_JZ);


                    sharesYjyg.setCode(SECURITY_CODE);
                    sharesYjyg.setName(SECURITY_NAME_ABBR);
                    sharesYjyg.setReportDate(REPORT_DATE);
                    sharesYjyg.setPreyearSamePeriod(PREYEAR_SAME_PERIOD);
                    sharesYjyg.setPredictType(PREDICT_TYPE);
                    sharesYjyg.setPredictFinanceCode(PREDICT_FINANCE_CODE);
                    sharesYjyg.setPredictFinance(PREDICT_FINANCE);
                    sharesYjyg.setPredictContent(PREDICT_CONTENT);
                    sharesYjyg.setPredictAmtUpper(PREDICT_AMT_UPPER);
                    sharesYjyg.setPredictAmtLower(PREDICT_AMT_LOWER);
                    sharesYjygs.add(sharesYjyg);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
       this.insertBatch(sharesYjygs);
    }
}
